/*
 * Copyright 2010 EES GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.eesgmbh.gimv.samples.googlechartapi.client.linecolors;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.shared.HandlerManager;

/**
 * Handles the {@link ColorListViewImpl} and fires a {@link ChangeLineColorsEvent} if
 * a color was selected.
 *
 * @author Sascha Hagedorn - EES GmbH - s.hagedorn@ees-gmbh.de
 *
 */
public class ColorListPresenter {

	public interface View {
		void setColor(int index);
		String getSelectedItem();
		void addChangeHandler(ChangeHandler handler);
	}

	private final View view;
	private final HandlerManager handlerManager;

	private int index = 0;

	public ColorListPresenter(HandlerManager handlerManager, View view) {
		this.view = view;
		this.view.addChangeHandler(new ColorListPresenterEventHandler());
		this.handlerManager = handlerManager;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	private void onChange(ChangeEvent event) {
		handlerManager.fireEvent(new ChangeLineColorsEvent(this.index, this.view.getSelectedItem()));
	}

	private class ColorListPresenterEventHandler implements ChangeHandler {

		public void onChange(ChangeEvent event) {
			ColorListPresenter.this.onChange(event);
		}

	}
}
